<div id="mod_input_{{each_input.unique_id}}">
  <form id="mod_input_form" method="post" action="/input">
  {{form_mod_input.csrf_token}}
  {{form_mod_input.input_id(value=each_input.unique_id)}}

  {% set form = form_mod_input %}
  {% set dict_options = dict_inputs[each_input.device] %}
  {% set each_device = each_input %}
  {% set unique_id = each_input.unique_id %}

  {% if each_input.device in ['RPiCPULoad', 'RPiFreeSpace'] -%}
    <input id="pre_output_id" name="pre_output_id" type="hidden" value="0">
    <input id="pre_output_duration" name="pre_output_duration" type="hidden" value="0">
  {% endif -%}

  <div class="row small-gutters" style="padding: 0.5em">
    <div class="col-auto">
      <button type="button" class="form-control btn btn-primary btn-block" data-dismiss="modal">{{_('Close')}}</button>
    </div>
    <div class="col-auto">
      <input onclick="return $(this).processRequest(this, 'input_mod');" name="input_mod" value="{{_('Save')}}" class="form-control btn btn-primary btn-block" type="button"/>
    </div>
    <div class="col-auto">
      <input onclick="return confirm('{{_('Are you sure you want to delete this?')}}') && $(this).processRequest(this, 'input_delete');" name="input_delete" value="{{_('Delete')}}" class="form-control btn btn-primary btn-block" type="button"/>
    </div>
    <div class="col-auto">
      <input onclick="return confirm('{{_('Are you sure you want to do this?')}}') &&  $(this).processRequest(this, 'input_duplicate');" name="input_duplicate" value="{{_('Duplicate')}}" class="form-control btn btn-primary btn-block" type="button"/>
    </div>
  </div>

  {% if each_input.device in dict_inputs %}

  <div class="row small-gutters" style="padding: 0.5em">

    {% if 'input_manufacturer' in dict_inputs[each_input.device] %}
    <div class="col-12">
      {{_("Manufacturer")}}: {{dict_inputs[each_input.device]['input_manufacturer']}}
    </div>
    {% endif %}

    {% if each_input.interface %}
    <div class="col-12">
      {{_("Interface")}}: {{each_input.interface}}
    </div>
    {% endif %}

    {% if 'measurements_name' in dict_inputs[each_input.device] %}
    <div class="col-12">
      {{_("Measurements")}}: {{dict_inputs[each_input.device]['measurements_name']}}
    </div>
    {% endif %}

    {% if 'dependencies_module' in dict_inputs[each_input.device] and dict_inputs[each_input.device]['dependencies_module'] %}
      <div class="col-12">
      {{_("Library")}}:
      {% for each_lib in dict_inputs[each_input.device]['dependencies_module'] -%}
        {% if each_lib[0] == 'pip-pypi' %}
            {% if each_lib[2].startswith("git+") %}
          {{each_lib[2]}}
            {% elif "==" in each_lib[2] %}
          <a href="https://pypi.org/project/{{each_lib[2].split('==')[0]}}/{{each_lib[2].split('==')[1]}}" target="_blank">{{each_lib[2].split('==')[0]}}</a>
            {% else %}
          <a href="https://pypi.org/project/{{each_lib[2]}}" target="_blank">{{each_lib[2]}}</a>
            {%  endif %}
        {% elif each_lib[0] == 'internal' %}
          {{each_lib[2]}}
        {% else %}
          {{each_lib[1]}}
        {% endif %}
        {%- if not loop.last %}, {% endif %}
      {% endfor %}
      </div>
    {% endif %}

    {% set manufacturer_url = 'url_manufacturer' in dict_inputs[each_input.device] and dict_inputs[each_input.device]['url_manufacturer'] %}
    {% set datasheet_url = 'url_datasheet' in dict_inputs[each_input.device] and dict_inputs[each_input.device]['url_datasheet'] %}
    {% set product_purchase_url = 'url_product_purchase' in dict_inputs[each_input.device] and dict_inputs[each_input.device]['url_product_purchase'] %}
    {% set additional_url = 'url_additional' in dict_inputs[each_input.device] and dict_inputs[each_input.device]['url_additional'] %}

    {% if manufacturer_url or datasheet_url or product_purchase_url or additional_url %}
    <div class="col-12">
      {{_("Literature") + ": "}}
      {%- if manufacturer_url -%}
        {{_("Manufacturer")}} {% for each_url in dict_inputs[each_input.device]['url_manufacturer'] %}<a href="{{each_url}}" target="_blank">Link {{loop.index}}</a>{{ ", " if not loop.last }}{% endfor %}
      {%- endif -%}
      {%- if datasheet_url -%}
        {% if manufacturer_url %}; {% endif -%}
        {{_("Datasheet")}} {% for each_url in dict_inputs[each_input.device]['url_datasheet'] %}<a href="{{each_url}}" target="_blank">Link {{loop.index}}</a>{{ ", " if not loop.last }}{% endfor %}
      {%- endif -%}
      {%- if product_purchase_url -%}
        {% if manufacturer_url or datasheet_url %}; {% endif -%}
        {{_("Product")}} {% for each_url in dict_inputs[each_input.device]['url_product_purchase'] %}<a href="{{each_url}}" target="_blank">Link {{loop.index}}</a>{{ ", " if not loop.last }}{% endfor %}
      {%- endif -%}
      {%- if additional_url -%}
        {% if manufacturer_url or datasheet_url or product_purchase_url %}; {% endif -%}
        {{_("Additional")}} {% for each_url in dict_inputs[each_input.device]['url_additional'] %}<a href="{{each_url}}" target="_blank">Link {{loop.index}}</a>{{ ", " if not loop.last }}{% endfor %}
      {%- endif -%}.
    </div>
    {% endif %}

    {% if 'message' in dict_inputs[each_input.device] %}
    <div class="col-12" style="padding-top: 0.3em">
      {{dict_inputs[each_input.device]['message']|safe}}
    </div>
    {% endif %}

    <div id="return_text_{{each_input.unique_id}}" class="col-12"></div>

  </div>

    {% if each_input.device != 'input_spacer' %}
  <div class="row small-gutters align-items-end" style="padding: 0.5em">
    <div class="col-12">
      <h5>{{_('Commands')}}</h5>
      <hr class="entry-break">
    </div>
    {% include 'pages/form_options/Custom_Actions_Message.html' %}
    <div class="col-auto small-gutters">
      <input onclick="return $(this).processRequest(this, 'input_acquire_measurements');" name="input_acquire_measurements" class="form-control btn btn-primary btn-block" type="button" value="{{_('Acquire Measurements Now')}}"/>
    </div>
      {% if each_input.device in custom_commands and custom_commands[each_input.device] %}
        {% set force_default_value = true %}
        {% set custom_options_values = none %}
        {% if 'custom_commands' in dict_options %}
          {% for each_option in dict_options['custom_commands'] %}
        {% include 'pages/form_options/Custom_Options.html' %}
          {% endfor %}
        {% endif %}
      {% endif %}
  </div>
    {% endif %}

  <div class="row small-gutters" style="padding: 0.5em">
    <div class="col-12">
      <h5>{{_('Options')}}</h5>
      <hr class="entry-break">
    </div>

    <div class="col-auto">
      {{form_mod_input.name.label(class_='control-label')}}
      <div>
        {{form_mod_input.name(class_='form-control', value=each_input.name, **{'title': dict_translation['name']['phrase']})}}
      </div>
    </div>

    <div class="col-auto">
      {{form_mod_input.unique_id.label(class_='control-label')}}
      <div>
        {{form_mod_input.unique_id(class_='form-control', value=each_input.unique_id, **{'title': dict_translation['unique_id']['phrase']})}}
      </div>
    </div>

    {% if each_input.device != 'input_spacer' %}

    <div class="col-auto">
      {{form_mod_input.log_level_debug.label(class_='control-label')}}
      <div class="input-group-text">
        <input id="log_level_debug" name="log_level_debug" type="checkbox" title="{{dict_translation['log_level_debug']['phrase']}}" value="y"{% if each_input.log_level_debug %} checked{% endif %}>
      </div>
    </div>

    {% include 'pages/form_options/Interface.html' %}
    {% include 'pages/form_options/GPIO.html' %}
    {% include 'pages/form_options/Bluetooth.html' %}
    {% include 'pages/form_options/I2C.html' %}
    {% include 'pages/form_options/FTDI.html' %}
    {% include 'pages/form_options/UART.html' %}
    {% include 'pages/form_options/Baud_Rate.html' %}
    {% include 'pages/form_options/Location.html' %}
    {% include 'pages/form_options/Period.html' %}
    {% include 'pages/form_options/Start_Offset.html' %}
    {% include 'pages/form_options/Pre_Output.html' %}
    {% include 'pages/form_options/Num_channels.html' %}
    {% include 'pages/form_options/Measurements_Enabled.html' %}

    {# TODO: move many of the below options from database columns to custom options at next major revision #}

    {% include 'pages/form_options/Single_Input_Math.html' %}
    {% include 'pages/form_options/EDGE.html' %}
    {% include 'pages/form_options/Times_Check.html' %}
    {% include 'pages/form_options/Deadline.html' %}
    {% include 'pages/form_options/Port.html' %}
    {% include 'pages/form_options/RPM_Pulses_Rev.html' %}
    {% include 'pages/form_options/Weighting.html' %}
    {% include 'pages/form_options/Sample_Time.html' %}
    {% include 'pages/form_options/SHT_Voltage.html' %}
    {% include 'pages/form_options/Pin_Clock.html' %}
    {% include 'pages/form_options/Ref_Ohm.html' %}
    {% include 'pages/form_options/Thermocouple_Type.html' %}
    {% include 'pages/form_options/Resolution.html' %}
    {% include 'pages/form_options/Resolution_2.html' %}
    {% include 'pages/form_options/Sensitivity.html' %}
    {% include 'pages/form_options/ADC_Gain.html' %}
    {% include 'pages/form_options/ADC_Resolution.html' %}
    {% include 'pages/form_options/ADC_Sample_Speed.html' %}
    {% include 'pages/form_options/Shell_Command.html' %}

    {% endif %}
  </div>

    {% set custom_options_values = custom_options_values_inputs %}

    {% if 'custom_options' in dict_options or
          'custom_options_message' in dict_options %}
      {% set force_default_value = false %}
  <div class="row small-gutters" style="padding: 0.5em">
    <div class="col-12">
      <h5>{{_('Custom Options')}}</h5>
      <hr class="entry-break">
    </div>
      {% if 'custom_options_message' in dict_options %}
    {% include 'pages/form_options/Custom_Options_Message.html' %}
      {% endif %}
      {% if 'custom_options' in dict_options %}
        {% for each_option in dict_options['custom_options'] %}
    {% include 'pages/form_options/Custom_Options.html' %}
        {% endfor %}
      {% endif %}
  </div>
    {% endif %}

    {%- set input_channel_exists = [] -%}
    {% for each_input_channel in input_channel if each_input_channel.input_id == each_input.unique_id %}
      {%- do input_channel_exists.append(1) -%}
    {% endfor %}

    {% if input_channel_exists %}

  <div class="row small-gutters" style="padding: 1em 0 0 0.5em">
    <div class="col-12">
      <h5>{{_('Channel Options')}}</h5>
      <hr class="entry-break">
    </div>
  </div>

    {% endif %}

    {% set custom_options_values = custom_options_values_input_channels %}
    {% set dict_options = dict_inputs[each_input.device] %}
    {% set unique_id = each_input.unique_id %}
    {% set each_device = each_input %}
    {% set controller_type = "Input" %}
    {% set force_default_value = true %}
    {% set channel_options = true %}

    {% for each_input_channel in input_channel if each_input_channel.input_id == each_input.unique_id %}

      {% set channel = each_input_channel.channel %}
      {% set name_prefix = each_device.unique_id + "_" + channel|string + "_" %}

  <hr/>
  <div class="row">
    <div class="col-auto">
      Channel {{each_input_channel.channel}}

      {%- if 'name' in custom_options_values[each_input.unique_id][each_input_channel.channel] and
           custom_options_values[each_input.unique_id][each_input_channel.channel]['name'] -%}
        {{': ' + custom_options_values[each_input.unique_id][each_input_channel.channel]['name']}}
      {%- endif -%}
    </div>
  </div>

      {% if custom_options_values[unique_id] %}

        {% if 'custom_channel_options_message' in dict_options %}
  <div class="row small-gutters" style="padding: 1em">
      <div class="col-12" style="padding-bottom: 0.5em">
        {{dict_options['custom_channel_options_message']|safe}}
      </div>
  </div>
        {% endif %}

        {% if 'custom_channel_options' in dict_options %}
  <div class="row small-gutters" style="padding: 1em">
          {% for each_option in dict_options['custom_channel_options'] %}
      {% include 'pages/form_options/Custom_Options.html' %}
          {% endfor %}
  </div>
        {% endif %}
      {% endif %}

    {% endfor %}

  {% endif %}

  {% if each_input.device != 'input_spacer' %}

  <div class="row small-gutters" style="border-bottom: 1px solid #ddd; padding: 0.5em 0.5em 0 0.5em">
    <div class="col-12">
      <h5>{{_('Measurement Settings')}}</h5>
      <hr class="entry-break">
    </div>
  </div>
    {% if "measurements_variable_amount" in dict_options and dict_options['measurements_variable_amount'] %}
      {% include 'pages/form_options/Measurements_Select.html' %}
    {% endif %}
  {% include 'pages/form_options/Measurements_Configure.html' %}
  {% endif %}

  </form>

  <div class="row align-items-end small-gutters" style="border-top: 1px solid #ddd; padding-top: 0.5em">
    <h5>{{_('Actions')}} <a href="https://kizniche.github.io/Mycodo/Actions" target="_blank"><span style="font-size: 16px" class="fas fa-question-circle"></span></a></h5>
  </div>

  <form method="post" action="/input">
    {{form_actions.csrf_token}}
    {{form_actions.device_id(value=each_input.unique_id)}}
    {{form_actions.function_type(value='input')}}

    <div class="row align-items-end small-gutters" style="padding: 0.5em 0 0.2em 1em">
      <div class="col-auto">
        <select class="selectpicker" data-style="btn btn-primary" data-live-search="true" title="{{_('Action')}}: {{dict_translation['select_one']['title']}}" id="action_type_{{each_input.unique_id}}" name="action_type">
        {% for value, name in choices_actions %}
          <option value="{{value}}">{{name}}</option>
        {% endfor %}
        </select>
      </div>
      <div class="col-auto">
        <input onclick="return $(this).processRequest(this, 'add_action');" name="add_action" value="{{_('Add')}}" class="form-control btn btn-primary btn-block" type="button"/>
      </div>
    </div>
  </form>

  {% set channel_options = false %}
  {% set post_action = "/input" %}

  <div id="mod_input_actions_{{each_input.unique_id}}">
    <div class="row small-gutters" style="border-bottom: 1px solid #ddd; padding: 0.3em">
      <div class="col-12">
        <strong>Usage:</strong>: Every Period of the Input, each Action is executed with the returned measurements.
      </div>
    </div>
  {% for each_action in action if each_action.function_id == each_input.unique_id %}
    {% include 'pages/actions.html' %}
  {% endfor %}
  </div>

</div>
